package com.wdc.heap;

import java.awt.image.renderable.RenderableImage;
import java.util.PriorityQueue;

public class MedianFinder {

    private PriorityQueue<Integer> left  = new PriorityQueue<>();   //默认创建出来是小顶堆

    //用lambda 表达式
    private PriorityQueue<Integer> right  = new PriorityQueue<>(
            (a,b) -> Integer.compareUnsigned(b,a)
    );

    public MedianFinder() {

    }
    
    public void addNum(int num) {
        if (left.size() == right.size()){
            right.offer(num);
            left.offer(right.poll());
        }else {
            left.offer(num);
            right.offer(left.poll());
        }

    }
    
    public double findMedian() {
        if (left.size() == right.size()){
            return (left.peek() + right.peek()) / 2.0;
        }else {
            return left.peek();
        }
    }
}